package people

import (
	
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/model/people"
    peopleReq "github.com/flipped-aurora/gin-vue-admin/server/model/people/request"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type GoodsApi struct {}



// CreateGoods 创建goods表
// @Tags Goods
// @Summary 创建goods表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body people.Goods true "创建goods表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /goods/createGoods [post]
func (goodsApi *GoodsApi) CreateGoods(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	var goods people.Goods
	err := c.ShouldBindJSON(&goods)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = goodsService.CreateGoods(ctx,&goods)
	if err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:" + err.Error(), c)
		return
	}
    response.OkWithMessage("创建成功", c)
}

// DeleteGoods 删除goods表
// @Tags Goods
// @Summary 删除goods表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body people.Goods true "删除goods表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /goods/deleteGoods [delete]
func (goodsApi *GoodsApi) DeleteGoods(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	ID := c.Query("ID")
	err := goodsService.DeleteGoods(ctx,ID)
	if err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteGoodsByIds 批量删除goods表
// @Tags Goods
// @Summary 批量删除goods表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /goods/deleteGoodsByIds [delete]
func (goodsApi *GoodsApi) DeleteGoodsByIds(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := goodsService.DeleteGoodsByIds(ctx,IDs)
	if err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateGoods 更新goods表
// @Tags Goods
// @Summary 更新goods表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body people.Goods true "更新goods表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /goods/updateGoods [put]
func (goodsApi *GoodsApi) UpdateGoods(c *gin.Context) {
    // 从ctx获取标准context进行业务行为
    ctx := c.Request.Context()

	var goods people.Goods
	err := c.ShouldBindJSON(&goods)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = goodsService.UpdateGoods(ctx,goods)
	if err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindGoods 用id查询goods表
// @Tags Goods
// @Summary 用id查询goods表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询goods表"
// @Success 200 {object} response.Response{data=people.Goods,msg=string} "查询成功"
// @Router /goods/findGoods [get]
func (goodsApi *GoodsApi) FindGoods(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	ID := c.Query("ID")
	regoods, err := goodsService.GetGoods(ctx,ID)
	if err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:" + err.Error(), c)
		return
	}
	response.OkWithData(regoods, c)
}
// GetGoodsList 分页获取goods表列表
// @Tags Goods
// @Summary 分页获取goods表列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query peopleReq.GoodsSearch true "分页获取goods表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /goods/getGoodsList [get]
func (goodsApi *GoodsApi) GetGoodsList(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	var pageInfo peopleReq.GoodsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := goodsService.GetGoodsInfoList(ctx,pageInfo)
	if err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败:" + err.Error(), c)
        return
    }
    response.OkWithDetailed(response.PageResult{
        List:     list,
        Total:    total,
        Page:     pageInfo.Page,
        PageSize: pageInfo.PageSize,
    }, "获取成功", c)
}

// GetGoodsPublic 不需要鉴权的goods表接口
// @Tags Goods
// @Summary 不需要鉴权的goods表接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /goods/getGoodsPublic [get]
func (goodsApi *GoodsApi) GetGoodsPublic(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

    // 此接口不需要鉴权
    // 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
    goodsService.GetGoodsPublic(ctx)
    response.OkWithDetailed(gin.H{
       "info": "不需要鉴权的goods表接口信息",
    }, "获取成功", c)
}
